import scenes
from math import sqrt
import pygame
game = None
MAX_LEVELS = 3
def tr(scene):
    return scenes.trans(game.scene, scene)

def new_game():
    game.scene = tr(scenes.Level(1))

def game_over():
    game.scene = tr(scenes.GameOver())
    
def next_lvl():
    lvl = game.scene.lvln
    if lvl+1 <= MAX_LEVELS:
        game.scene = tr(scenes.Level(lvl+1))
        return
    game.scene = tr(scenes.Win())

def record_table():
    game.scene = tr(scenes.Intro())


#===================================
#============BONUSES================
#===================================
def slow(*args):
    for ball in game.scene.balls:
        bs = ball.speed
        ball.speed *= 5/sqrt(bs.dot(bs))

def split(*args):
    for ball in game.scene.balls:
        if len(game.scene.balls) > 100:
            return
        b = ball.copy()
        b.speed[0] = -b.speed[0]
        game.scene.balls.add(b)
        game.scene.add(b)

def large(pad):
    
    if hasattr(pad,'large') and pad.large:
        pad.timer = max (0, pad.timer - 10*60)
        return
    
    pad.old_rect = pad.rect.copy()
    pad.rect.width += 100
    
    pad.old_img = pad.image
    pad.image = pygame.Surface(pad.rect.size)
    pad.image.fill((0,255,0))
    
    pad.timer = 1
    pad.old_upd = pad.update
    pad.large = True
    f=pygame.font.Font('Ubuntu-B.ttf',20)
    def upd():
        pad.old_upd()
        text = str((600-pad.timer)/60+1)
        pad.image.fill((0,255,0))
        pad.image.blit( f.render(text,1,(0,0,0)),(pad.rect.width/2-7,0))
        if pad.timer > 600:
            pad.large = False
            pad.update = pad.old_upd
            pad.image = pad.old_img
            pad.rect.width = pad.old_rect.width
        pad.timer += 1
    pad.update = upd
        

